from flask import Flask, render_template, request, jsonify
import os
from werkzeug.utils import secure_filename
from model_predict import predict_image

app = Flask(__name__)

UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# 确保上传文件夹存在
os.makedirs(UPLOAD_FOLDER, exist_ok=True)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    if 'image' not in request.files:
        return jsonify({'error': '没有上传文件'})

    image = request.files['image']

    if image.filename == '':
        return jsonify({'error': '未选择文件'})

    filename = secure_filename(image.filename)
    save_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
    image.save(save_path)

    result = predict_image(save_path)
    return jsonify(result)

if __name__ == '__main__':
    app.run(debug=True)



# import gradio as gr
# from model_predict import predict_image
#
# # 如果你的 predict_image 是接收图像路径的，请改成接受 PIL.Image 对象
# def classify_image(image):
#     # 将 PIL 图像保存为临时文件
#     import tempfile
#     import os
#     with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp:
#         image.save(temp.name)
#         temp_path = temp.name
#
#     result = predict_image(temp_path)
#
#     os.remove(temp_path)  # 删除临时文件
#     return f"预测结果：{result}"
#
# interface = gr.Interface(
#     fn=classify_image,
#     inputs=gr.Image(type="pil", label="上传眼底图像"),
#     outputs=gr.Textbox(label="预测结果"),
#     title="眼部疾病智能识别系统",
#     description="上传一张眼底图像，我将识别可能的眼部疾病。",
#     theme="default"
# )
#
# if __name__ == "__main__":
#     interface.launch()


